﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfOrderList : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //获取订单列表
        private void getOrderList()
        {
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo ordersInfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                int pageIndex = 1;
                int pageSize = 10;
                int.TryParse(this.GetParamHtValue("pIndex"), out pageIndex);
                string solderid = msBsc.SvRef.UserInfo.Operator;
                if (string.IsNullOrEmpty(solderid))
                {
                    this.ExecJScript("$('#orderList').html(\"<p style='width: 200px; margin-left: 30px;' class='car-price'>没有查询到任何相关信息...</p>\");$('#divPage').html('');");
                    return;
                }
                int orderStatus = 0;
                int.TryParse(this.GetParamHtValue("orderStatus"), out orderStatus);

                DataSet ds = ordersInfo.GetOrderList(solderid, "", (OrderStatusEnum)orderStatus, pageIndex, pageSize);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript("$('#orderList').html(\"<p style='width: 200px; margin-left: 30px;' class='car-price'>没有查询到任何相关信息...</p>\");$('#divPage').html('');");
                    return;
                }

                //获取订单各项数量
                DataTable dt = ordersInfo.GetBuyerAllSelectCount(solderid);
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 5, "");
                this.ExecJScript(@"setAllCount(" + retJson + ");");

                retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                
                this.ExecJScript(@"
                            var _html = $('#svc_list_temp_order').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#orderList').html(render);
                            });jsPage('divPage', " + ds.Tables[0].Rows[0]["DATACOUNT"] + ", " + pageSize + ", " + pageIndex + ", 'goPage');");
            }
        }

        private void BuyerAlipy()
        {
            //是否 已经登录
            string solderid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(solderid))
            {
                this.ExecJScript(@"layer.alert('登录已过期，请重新操作', 3);");
                return;
            }
            //是否有订单
            string orderid = this.GetParamHtValue("orderid");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('该订单号不存在,请重新操作.', 3);");
                return;
            }
            using (CBizDjkChargeTradingLog ctLog = new CBizDjkChargeTradingLog())
            {
                string[] stCustomer = ctLog.AddCustomerTradingForPay(orderid);
                if (stCustomer[0] == "OK")
                {
                    this.ExecJScript(@"window.location.href ='/Alipay/default.aspx?ID=" + stCustomer[1] + "';");
                }
                else
                {
                    this.ExecJScript(@"layer.alert('" + stCustomer[1] + "', 3);");
                }

            }
        }
        //删除订单
        public void DeleteOrder()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderinfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                string msgs = orderinfo.DeleteOrder(userid, orderid,false);
                if (msgs!="OK")
                {
                    this.ExecJScript(@"layer.alert('" + msgs + "', 3);");
                }
                else
                {
                    this.ExecJScript(@"success();");
                }
            }
        }
        //取消订单
        public void ClerOrder()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderinfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                string[] msgs = orderinfo.UpdateBuyerOrderStatus(userid, orderid, OrderStatusEnum.CancelOrder);
                if (!Convert.ToBoolean(msgs[0]))
                {
                    this.ExecJScript(@"layer.alert('" + msgs[1] + "', 3);");
                }
                else
                {
                    this.ExecJScript(@"success();");
                }
            }
        }
        //取消订单 退款请求
        public void ClerRefund()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }
            using (CBizCustomerRefund refund = new CBizCustomerRefund())
            {
                string msgs = refund.CancelRefund(orderid);
                if (msgs != "OK")
                {
                    this.ExecJScript(@"layer.alert('" + msgs + "', 3);");
                }
                else
                {
                    this.ExecJScript(@"success();");
                }
            }
        }

        //ManualCloseAccount 服务完成
        public void OkServer()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录失效请重新登录.', 3);");
                return;
            }
            string orderid = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderid))
            {
                this.ExecJScript(@"layer.alert('订单ID错误,请重新操作.', 3);");
                return;
            }

            using (CBizDjkChargeTradingLog chareg = new CBizDjkChargeTradingLog())
            {
                string msg = chareg.ManualCloseAccount(orderid);
                if (msg != "OK")
                {
                    this.ExecJScript(@"layer.alert('" + msg + "', 3);");
                    return;
                }
                this.ExecJScript(@"success();");

            }
        }


        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                //初始化
                getOrderList();
            }
            else if (this.strAction == "GOA")
            {
                BuyerAlipy();
            }
            else if (this.strAction == "del")
            {
                DeleteOrder();
            }
            else if (this.strAction == "cler")
            {
                ClerOrder();
            }
            else if (this.strAction == "clerRefund")
            {
                ClerRefund();
            }
            else if (this.strAction == "okserver")
            {
                OkServer();
            }

        }
    }
}